package com.cy.ppj.comm.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;


/**
 * 导出Excel表格工具类
 *
 * @author: tanyilin
 * @create: 2018年10月18日 下午6:36:00
 */
public class ExportExcelUtils<T> {
 
	/**
	 * 导出Excel
	 * 
	 * @param title 标题
	 * @param headers 列名
	 * @param fieldNames 对应实体的属性名
	 * @param data 数据列表
	 * @param out 输出流
	 */
	public void exportExcel(String title, String[] headers, String[] fieldNames, List<T> data, OutputStream out) {
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		//生成一个表格
		HSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为30个字节
		sheet.setDefaultColumnWidth(30);
		// 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		style.setBorderBottom(BorderStyle.THIN);
		style.setBorderLeft(BorderStyle.THIN);
		style.setBorderRight(BorderStyle.THIN);
		style.setBorderTop(BorderStyle.THIN);
		style.setAlignment(HorizontalAlignment.CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 20);
		font.setBold(true);
		// 把字体应用到当前的样式
		style.setFont(font);
		
		// 生成并设置另一个样式
		HSSFCellStyle style2 = workbook.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		style2.setBorderBottom(BorderStyle.THIN);
		style2.setBorderLeft(BorderStyle.THIN);
		style2.setBorderRight(BorderStyle.THIN);
		style2.setBorderTop(BorderStyle.THIN);
		style2.setAlignment(HorizontalAlignment.CENTER);
		style2.setVerticalAlignment(VerticalAlignment.CENTER);
		// 生成另一个字体
		HSSFFont font2 = workbook.createFont();
		font2.setBold(false);
		font2.setFontHeightInPoints((short) 16);
		// 把字体应用到当前的样式
		style2.setFont(font2);
		
		// 声明一个画图的顶级管理器
//		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//		// 定义注释的大小和位置,详见文档
//		HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
//		// 设置注释内容
//		comment.setString(new HSSFRichTextString("可以在POI中添加注释！"));
//		// 设置注释作者，当鼠标移动到单元格上是可以在状态栏中看到该内容.
//		comment.setAuthor("tyl");
		
		// 产生表格标题行
		HSSFRow row = sheet.createRow(0);
		for(int i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		
		// 遍历集合数据，产生数据行
		Iterator<T> it = data.iterator();
		int index = 0;
		while(it.hasNext()) {
			index++;
			// 创建数据行
			row = sheet.createRow(index);
			T t = (T) it.next();
			for(int i = 0; i < fieldNames.length; i++) {
				// 遍历字段属性名数组，放进数据行的每一个单元格
				// 创建单元格
				HSSFCell cell = row.createCell(i);
				cell.setCellStyle(style2);
				// 得到字段属性名 
				String fifieldName = fieldNames[i];
				// 拼凑相应的 getXxx()方法
				String getMethodName = "get".concat(fifieldName.substring(0, 1).toUpperCase()).concat(fifieldName.substring(1));
				Class tClass = t.getClass();
				try {
					Method getMethod = tClass.getMethod(getMethodName, new Class[] {}) ;
					Object value = getMethod.invoke(t, new Object[] {});
					// 判断值的类型后进行强制类型转换
					String textValue = "";
					if(value != null) {
						if(value instanceof Date) {
							Date date = (Date) value;
							textValue = CommonUtils.dateFormat(date);
						}else {
							textValue = value.toString();
						}
					}
					HSSFRichTextString richString = new HSSFRichTextString(textValue);
					cell.setCellValue(richString);
					
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				}catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				
			}
		}
		
		// 写出 out 
		try {
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	
}

